import { http } from "msw";

import { withResponseSuccess } from "@/mocks/util";
import { userRest } from "@/api/user";

function generateAccessToken() {
  // 使用随机字符生成一个模拟的 accessToken
  const characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let accessToken = "";
  for (let i = 0; i < 32; i++) {
    accessToken += characters.charAt(
      Math.floor(Math.random() * characters.length)
    );
  }
  return accessToken;
}

const login = http.post(
  userRest.login,
  withResponseSuccess<{ name: string; accessToken: string }, {}>(() => ({
    uid: 100,
    name: "vincent",
    avatar:
      "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png",
    access: "canAdmin",
    accessToken: generateAccessToken(),
  }))
);

const logout = http.post(
  userRest.logout,
  withResponseSuccess(() => ({
    data: null,
  }))
);

const checkLogin = http.post(
  userRest.checkLogin,
  withResponseSuccess<{ name: string; uid: number }, {}>(() => ({
    name: "vincent",
    uid: 100,
    status: "ok",
  }))
);

const getFakeCaptcha = http.post(
  userRest.captcha,
  withResponseSuccess<{ status: string; code: number }, {}>(() => ({
    code: 840110,
    status: "ok",
  }))
);

const getUserInfo = http.post(
  userRest.userInfo,
  withResponseSuccess<{ name: string; uid: number }, {}>(() => ({
    name: "vincent",
    avatar:
      "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png",
    email: "antdesign@alipay.com",
    uid: 100,
    access: "admin",
    address: "西湖区工专路 77 号",
    phone: "0752-268888888",
  }))
);

const getUserMenu = http.post(
  userRest.menus,
  withResponseSuccess(() => {
    return {
      menus: [
        {
          id: 1,
          name: "login",
          path: "/user/login",
        },
        {
          id: 2,
          path: "/welcome",
          name: "welcome",
        },
        {
          id: 3,
          path: "/admin",
          name: "admin",
        },
        {
          id: 4,
          path: "/admin/sub-page",
          name: "sub-page",
          operations: [1, 3, 4]
        },
        {
          id: 5,
          path: "/list",
          name: "list.table-list",
        },
        //{
        //  id: 6,
        //  path: "/list/detail",
        //  name: "list.table-list",
        //},
      ]
    };
  })
);

const userApi = [getUserMenu, getUserInfo, getFakeCaptcha, checkLogin, login, logout];

export default userApi;
